package base
{
	import config.Config;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BaseBitmapDataClip extends Sprite
	{
		
		public function BaseBitmapDataClip(param1:Array, param2:int, param3:int, param4:Point)
		{
			frameStopCount = [[4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4]];
			frameCount = [4, 6, 6, 6, 6, 6, 6, 6];
			bmdArray = [];
			bmSprite = new Sprite();
			super();
			curPoint = param4;
			bmWidth = param2;
			bmHeight = param3;
			bmdArray = param1;
			addChild(bmSprite);
		}
		
		public var isStopFrame:Boolean = false;//是否停止帧动画
		public var bmSprite:Sprite;
		protected var frameStopCount:Array;//帧停留时间
		protected var frameCount:Array;//帧数
		protected var frameCountMaxLen:uint;//帧数最大值
		protected var curFrameStopCount:int;//当前帧停留时间
		protected var curPoint:Point;//当前帧坐标
		protected var curKeyFrameIndex:uint = 0;//当前帧索引
		protected var enterFrameFunc:Function;//进入帧回调
		protected var exitFrameFunc:Function;//离开帧回调
		protected var addFrameScriptWhenFrameOver:Function;//帧结束回调
		protected var bm:Bitmap;//帧图片
		protected var bmWidth:int;
		protected var bmHeight:int;
		protected var offsetX:int;//偏移量X
		protected var offsetY:int;//偏移量Y
		protected var state:String = "idle";
		protected var bmdArray:Array;//帧图片bmd数据
		private var direct:int = 0;
		private var _framer:int = 0;//帧计数器
		
		/**
		 * 设置进入帧回调
		 * @param param1 回调函数
		 * @param param2 回调函数
		 */
		public function setEnterFrameCallBack(param1:Function, param2:Function):void
		{
			enterFrameFunc = param1;
			exitFrameFunc = param2;
		}
		
		public function setAddScriptWhenFrameOver(param1:Function):void
		{
			addFrameScriptWhenFrameOver = param1;
		}
		
		public function setFramePointX(param1:int):void
		{
			curPoint.x = param1;
			frameShow();
		}
		
		public function setFramePointY(param1:int):void
		{
			curPoint.y = param1;
			frameShow();
		}
		
		/**
		 * 帧动画执行
		 */
		public function step():void
		{
			_framer++;
			/*----------------有待商榷的危险代码----------------*/
			if(_framer % (Config.getIns().frameClips / 30) != 0)
			{
				return;
			}
			/*----------------有待商榷的危险代码----------------*/
			var _loc1_:* = 0;
			var _loc2_:* = null;
			if (!isStopFrame)
			{
				addFrameScriptEnterEveryFrame();
				if (curFrameStopCount > 1)
				{
					--curFrameStopCount;
				}
				else
				{
					if (frameCount[curPoint.y] is Array)
					{
						_loc2_ = frameCount[curPoint.y] as Array;
						_loc1_ = uint(_loc2_[curPoint.x] - 1);
					}
					else
					{
						_loc1_ = uint(frameCount[curPoint.y] - 1);
					}
					if (curKeyFrameIndex >= _loc1_)
					{
						if (addFrameScriptWhenFrameOver != null)
						{
							addFrameScriptWhenFrameOver(curPoint.y);
						}
						curKeyFrameIndex = 0;
					}
					else
					{
						var _loc3_:* = uint((frameStopCount[curPoint.y] as Array).length);
						if (curPoint.x >= _loc3_ - 1)
						{
							curPoint.x = 0;
						}
						else
						{
							++curPoint.x;
						}
						++curKeyFrameIndex;
						frameShow();
					}
				}
				addFrameScriptExitEveryFrame();
			}
		}
		
		public function setState(param1:String):void
		{
			if (state != param1)
			{
				state = param1;
				curKeyFrameIndex = 0;
			}
		}
		
		public function getState():String
		{
			return state;
		}
		
		public function setFrameStopCount(param1:Array):void
		{
			frameStopCount = param1;
		}
		
		public function getFrameStopCount():Array
		{
			return frameStopCount;
		}
		
		public function setFrameCount(param1:Array):void
		{
			frameCount = param1;
		}
		
		public function getCurPoint():Point
		{
			return curPoint;
		}
		
		public function resetCurFrameStopCount():void
		{
			curFrameStopCount = frameStopCount[curPoint.y][curPoint.x];
		}
		
		public function getCurFrameCount():int
		{
			return curFrameStopCount;
		}
		
		public function setCurFrameCount(param1:int):void
		{
			curFrameStopCount = param1;
		}
		
		public function stopFrame():void
		{
			isStopFrame = true;
		}
		
		public function continueFrame():void
		{
			isStopFrame = false;
		}
		
		public function turnRight():void
		{
			setDirect(1);
		}
		
		public function turnLeft():void
		{
			setDirect(0);
		}
		
		public function setOffsetXY(param1:int, param2:int):void
		{
			offsetX = param1;
			offsetY = param2;
			setXYByDirect();
		}
		
		public function setXYByDirect():void
		{
			if (direct == 0)
			{
				x = -Number(bmWidth) / 2 - Number(offsetX);
			}
			else
			{
				x = -Number(bmWidth) / 2 + offsetX;
			}
			y = -Number(bmHeight) / 2 + offsetY;
		}
		
		public function replaceBitmapDataByName(param1:String, param2:Array):void
		{
			var _loc3_:* = null;
			var _loc4_:int = 0;
			while (_loc4_ < bmdArray.length)
			{
				_loc3_ = bmdArray[_loc4_] as Object;
				if (_loc3_.name == param1)
				{
					_loc3_.source = param2;
				}
				bmdArray[_loc4_] = _loc3_;
				_loc4_++;
			}
		}
		
		public function hide():void
		{
			visible = false;
		}
		
		public function show():void
		{
			visible = true;
		}
		
		public function setDirect(param1:uint):void
		{
			if (getDirect() != param1)
			{
				direct = param1;
				setXYByDirect();
				refreshCurFrame();
			}
		}
		
		public function getCurKeyFrameIndex():uint
		{
			return curKeyFrameIndex;
		}
		
		public function getDirect():uint
		{
			return direct;
		}
		
		public function destroy():void
		{
			bmdArray.length = 0;
			enterFrameFunc = null;
			exitFrameFunc = null;
			addFrameScriptWhenFrameOver = null;
			if (parent)
			{
				parent.removeChild(this);
			}
		}
		
		protected function frameShow():void
		{
			curFrameStopCount = frameStopCount[curPoint.y][curPoint.x];
			refreshCurFrame();
		}
		
		protected function refreshCurFrame():void
		{
			var _loc1_:* = null;
			while (bmSprite.numChildren)
			{
				_loc1_ = bmSprite.getChildAt(0) as Bitmap;
				_loc1_.bitmapData.dispose();
				bmSprite.removeChild(_loc1_);
			}
			_loc1_ = new Bitmap(getCurFrameBitmapData());
			bmSprite.addChild(_loc1_);
		}
		
		protected function getCurFrameBitmapData():BitmapData
		{
			var _loc4_:* = null;
			var _loc5_:* = null;
			var _loc6_:* = null;
			var _loc7_:* = null;
			var newBmd:BitmapData = new BitmapData(bmWidth, bmHeight, true, 0xFFFFFF);
			for each (var _loc2_:* in bmdArray)
			{
				//每一个bbdc都是由两个部分构成，即衣服和武器
				var arr:Array = _loc2_.source as Array;
				if (arr.length != 2)
				{
					continue;
				}
				//如果是BitmapData执行的逻辑
				if (arr[0] is BitmapData)
				{
					_loc4_ = arr[0] as BitmapData;
					_loc5_ = arr[1] as BitmapData;
					frameCountMaxLen = Number(_loc4_.width) / Number(bmWidth);
					if (curPoint.x <= frameCountMaxLen - 1)
					{
						if (direct == 1)
						{
							newBmd.copyPixels(_loc5_, new Rectangle(Number(bmWidth) * (frameCountMaxLen - (curPoint.x + 1)), Number(bmHeight) * Number(curPoint.y), bmWidth, bmHeight), new Point(0, 0), null, null, true);
						}
						else
						{
							newBmd.copyPixels(_loc4_, new Rectangle(Number(bmWidth) * Number(curPoint.x), Number(bmHeight) * Number(curPoint.y), bmWidth, bmHeight), new Point(0, 0), null, null, true);
						}
					}
				}
				//如果是Array执行的逻辑
				else if (arr[0] is Array)
				{
					_loc6_ = arr[0] as Array;
					_loc7_ = arr[1] as Array;
					_loc4_ = _loc6_[curPoint.y][curPoint.x];
					_loc5_ = _loc7_[curPoint.y][curPoint.x];
					if (_loc4_ && _loc5_)
					{
						if (direct == 1)
						{
							newBmd.copyPixels(_loc5_, _loc5_.rect, new Point(0, 0), null, null, true);
						}
						else
						{
							newBmd.copyPixels(_loc4_, _loc4_.rect, new Point(0, 0), null, null, true);
						}
					}
				}
			}
			return newBmd;
		}
		
		protected function addFrameScriptEnterEveryFrame():void
		{
			if (enterFrameFunc != null)
			{
				enterFrameFunc(curPoint);
			}
		}
		
		protected function addFrameScriptExitEveryFrame():void
		{
			if (exitFrameFunc != null)
			{
				exitFrameFunc(curPoint);
			}
		}
	}
}
